package com.business.customer.service;

import java.util.*;

import com.business.system.model.auto.TsysUser;
import com.business.system.service.SysUserService;
import org.apache.commons.lang3.StringUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import com.business.system.common.base.BaseService;
import com.business.system.common.support.Convert;
import com.business.customer.mapper.auto.CustomerInfoMapper;
import com.business.customer.model.auto.CustomerInfo;
import com.business.customer.model.auto.CustomerInfoExample;
import com.business.system.model.custom.Tablepar;
import com.business.system.util.SnowflakeIdWorker;

/**
 * 客户信息 CustomerInfoService
 * @Title: CustomerInfoService.java 
 * @Package com.business.customer.service 
 * @author LuJingyuan_自动生成
 * @email 674326842@qq.com
 * @date 2020-12-08 10:19:55  
 **/
@Service
public class CustomerInfoService implements BaseService<CustomerInfo, CustomerInfoExample>{
	@Autowired
	private CustomerInfoMapper customerInfoMapper;

	@Autowired
	private SysUserService sysUserService;
	
      	   	      	      	      	      	      	      	      	      	      	      	      	
	/**
	 * 分页查询
	 * @param pageNum
	 * @param pageSize
	 * @return
	 */
	 public PageInfo<CustomerInfo> list(Tablepar tablepar,String name){
	        CustomerInfoExample testExample=new CustomerInfoExample();
	        testExample.setOrderByClause("id ASC");
	        if(name!=null&&!"".equals(name)){
	        	testExample.createCriteria().andUserIdLike("%"+name+"%");
	        }

	        PageHelper.startPage(tablepar.getPageNum(), tablepar.getPageSize());
	        List<CustomerInfo> list= customerInfoMapper.selectByExample(testExample);
	        PageInfo<CustomerInfo> pageInfo = new PageInfo<CustomerInfo>(list);
	        return  pageInfo;
	 }

	@Override
	public int deleteByPrimaryKey(String ids) {
				
			List<String> lista=Convert.toListStrArray(ids);
			CustomerInfoExample example=new CustomerInfoExample();
			example.createCriteria().andIdIn(lista);
			return customerInfoMapper.deleteByExample(example);
			
				
	}
	
	
	@Override
	public CustomerInfo selectByPrimaryKey(String id) {
				
			return customerInfoMapper.selectByPrimaryKey(id);
				
	}

	
	@Override
	public int updateByPrimaryKeySelective(CustomerInfo record) {
		return customerInfoMapper.updateByPrimaryKeySelective(record);
	}
	
	
	/**
	 * 添加
	 */
	@Override
	public int insertSelective(CustomerInfo record) {
				
		//添加雪花主键id
		record.setId(SnowflakeIdWorker.getUUID());
			
				
		return customerInfoMapper.insertSelective(record);
	}
	
	
	@Override
	public int updateByExampleSelective(CustomerInfo record, CustomerInfoExample example) {
		
		return customerInfoMapper.updateByExampleSelective(record, example);
	}

	
	@Override
	public int updateByExample(CustomerInfo record, CustomerInfoExample example) {
		
		return customerInfoMapper.updateByExample(record, example);
	}

	@Override
	public List<CustomerInfo> selectByExample(CustomerInfoExample example) {
		
		return customerInfoMapper.selectByExample(example);
	}

	
	@Override
	public long countByExample(CustomerInfoExample example) {
		
		return customerInfoMapper.countByExample(example);
	}

	
	@Override
	public int deleteByExample(CustomerInfoExample example) {
		
		return customerInfoMapper.deleteByExample(example);
	}
	
	/**
	 * 检查name
	 * @param customerInfo
	 * @return
	 */
	public int checkNameUnique(CustomerInfo customerInfo){
		CustomerInfoExample example=new CustomerInfoExample();
		example.createCriteria().andUserIdEqualTo(customerInfo.getUserId());
		List<CustomerInfo> list=customerInfoMapper.selectByExample(example);
		return list.size();
	}

	/**
	 * 根据map查询list
	 * @param map
	 * @return
	 */
	public List<CustomerInfo> selectByMap(Map<String,String> map){
		return customerInfoMapper.selectByMap(map);
	}


	/**
	 * 根据微信用户注册页面传的值初始化客户信息
	 * @param map
	 * @return
	 */
	public CustomerInfo initWechatCustomer(Map<String,String> map){
		String registerFrom=map.get("register_from");
		Map<String,String> selectMap=new HashMap<String,String>();
		if(StringUtils.isNotBlank(map.get("openid"))){
			selectMap.put("openid",map.get("openid"));
			CustomerInfo customerInfo;
			List<CustomerInfo> list=this.selectByMap(selectMap);
			if(list!=null&&list.size()>0){
				customerInfo=list.get(0);
			}else{
				customerInfo=new CustomerInfo();
				customerInfo.setCreateTime(new Date());
			}
			customerInfo.setLastLoginTime(new Date());
			customerInfo.setOpenid(map.get("openid"));
			customerInfo.setName(map.get("name"));
			customerInfo.setAddress(map.get("address"));
			customerInfo.setRegisterFrom("wechat");
			customerInfo.setIsActive(1);

			//初始化系统用户
			TsysUser user=null;
			if(StringUtils.isNotBlank(customerInfo.getUserId())){
				user=sysUserService.selectByPrimaryKey(customerInfo.getUserId());
			}
			if(user==null){
				user=new TsysUser();
				user.setNickname(customerInfo.getName());
				user.setId(SnowflakeIdWorker.getUUID());
				sysUserService.insertSelective(user);
				customerInfo.setUserId(user.getId());
			}else{
				user.setNickname(customerInfo.getName());
			}
			if(StringUtils.isNotBlank(customerInfo.getId())){
				this.updateByPrimaryKeySelective(customerInfo);
			}else{
				this.insertSelective(customerInfo);
			}
			return customerInfo;
		}
		return null;
	}

	public CustomerInfo getUniqueCustomerByUserId(String userId){
		return customerInfoMapper.getUniqueCustomerByUserId(userId);
	}
}
